package devicedbgui;

import devicedbgui.data.ExeDAO;
import devicedbgui.data.ExeStats;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

/** Displays statistics pertaining to exe files */
public class ExeStatsPanel extends javax.swing.JPanel {
    Hashtable<Integer, ExeStats> htExeStats = new Hashtable<Integer, ExeStats>();
     GUI gui;
    /** Constructor creates all components belonging to panel */
    public ExeStatsPanel(GUI gui) {
        initComponents();
        this.gui = gui;
        loadData(gui);
    }
    /** Displays error messages */
    public void showError(JFrame parent, String error)
    {
        JOptionPane.showMessageDialog(parent, error);
    }
    /** Displays test case data from database */
    private void loadData(GUI gui)
    {
        ExeDAO dd = new ExeDAO(gui.connection);
        try {
            ArrayList<ExeStats> records = dd.getExeStats();
            DefaultTableModel model = (DefaultTableModel)tblExeStats.getModel();
            model.getDataVector().clear();
            int index = 0;   
            htExeStats.clear();
            for (ExeStats item : records){ 
                htExeStats.put(new Integer(index++), item);
                model.addRow(new Object[]{item.exeDescription, item.numberOfFailed, item.numberOfPassed});
            }
            
        }
        catch (Exception ex){
            showError(gui.frame, ex.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblExeStats = new javax.swing.JTable(){    public Component prepareRenderer(TableCellRenderer renderer,
            int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (rowIndex % 2 == 0 && !tblExeStats.isRowSelected(rowIndex)) {
                c.setBackground(new Color(237, 242, 251));
            }
            else if (tblExeStats.isRowSelected(rowIndex)){c.setBackground(new Color(184, 207, 229));}
            else { c.setBackground(getBackground());}
            return c;
        }
    }
    ;
    testCasebtn = new javax.swing.JButton();

    tblExeStats.setAutoCreateRowSorter(true);
    tblExeStats.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null}
        },
        new String [] {
            "Exe Description", "Number of times Exe Failed", "Number of times Exe Passed"
        }
    ));
    tblExeStats.setGridColor(new java.awt.Color(204, 204, 255));
    tblExeStats.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(tblExeStats);

    testCasebtn.setText("Test Cases");
    testCasebtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            testCasebtnActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(testCasebtn, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(testCasebtn)))
            .addContainerGap())
    );
    }// </editor-fold>//GEN-END:initComponents
/** Displays further test cases related to selected exe*/
private void testCasebtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testCasebtnActionPerformed
  int[] selected = tblExeStats.getSelectedRows();  
    if (selected.length == 0){
        JOptionPane.showMessageDialog(gui.frame, "No item has been selected, please select an item");
        return;
    }
    
    ExeStats item = htExeStats.get(new Integer(selected[0]));
    
    if (item == null)
    {
        JOptionPane.showMessageDialog(gui.frame, "No item has been selected, please select an item");
        return;
    }
    
    // show TestCaseStats dialog
    TestCaseStatsPanel p = new TestCaseStatsPanel(gui,item.exeDescription);
     gui.tab.add(p);
   gui.tab.setSelectedComponent(p); 
   gui.tab.setTabComponentAt( gui.tab.getSelectedIndex(), gui.TabSet("View Test Case Stats") );
   
}//GEN-LAST:event_testCasebtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblExeStats;
    private javax.swing.JButton testCasebtn;
    // End of variables declaration//GEN-END:variables

}
